package com.ylkj.wx.utils;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

public class QiniuUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(QiniuUtil.class);
	private static final String ACCESS_KEY = "7Td3pnkKErqnQnUCSsqmD0aARcbF9q6pWPrNxm3B";
	private static final String SECRET_KEY = "TXJL77pDQaC1GsUO5lC-Tev7k6W2_B2W28XFsgKm";

		  //设置好账号的ACCESS_KEY和SECRET_KEY
		  /*String ACCESS_KEY = "Access_Key";
		  String SECRET_KEY = "Secret_Key";*/
		  //要上传的空间
		  String bucketname = "fileylkj";
		  //上传到七牛后保存的文件名
		  String key = "my-java.png";
		  //上传文件的路径
		  String FilePath = "/wxAnswer";
		  
		  

		  //密钥配置
		  Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
		  //创建上传对象
		  UploadManager uploadManager = new UploadManager();

		  //简单上传，使用默认策略，只需要设置上传的空间名就可以了
		  public String getUpToken(){
		      return auth.uploadToken(bucketname);
		  }

		  public void upload(byte[] fileStream,String fileName) throws IOException{
		    try {
		      //调用put方法上传
		      Response res = uploadManager.put(fileStream, fileName, getUpToken());
		      //打印返回的信息
		      System.out.println(res.bodyString()); 
		      } catch (QiniuException e) {
		          Response r = e.response;
		          // 请求失败时打印的异常的信息
		          System.out.println(r.toString());
		          try {
		              //响应的文本信息
		            System.out.println(r.bodyString());
		          } catch (QiniuException e1) {
		              e1.printStackTrace();
		          }
		      }       
		  }
		  
		  
		  public String downloadFile(String fileName){
			  String url = "http://oextei5g7.bkt.clouddn.com/"+fileName;
			     //调用privateDownloadUrl方法生成下载链接,第二个参数可以设置Token的过期时间
			     String downloadRUL = auth.privateDownloadUrl(url,3600);
			     System.out.println(downloadRUL);
				return downloadRUL;
			   }
		 
		

	/* public static void main(String args[]) throws IOException{  
		    new QiniuUtil().upload();
		  }*/


}
